﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using System.Web.UI.WebControls;

namespace CarLoanSystem
{
    public partial class panelPayment : UserControl
    {
        private static EmployeeItem empItem;
        public panelPayment()
        {
            InitializeComponent();
            this.grvPayment.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void setEmployee(EmployeeItem item)
        {
            empItem = item;
        }

        private void genGridview()
        {
            // gen Grideview
            grvPayment.AutoGenerateColumns = false;

            //Set Columns Count
            grvPayment.ColumnCount = 7;

            grvPayment.Columns[0].Width = 45;

            //Add Columns
            grvPayment.Columns[1].Name = "PAYMENTS_ID";
            grvPayment.Columns[1].HeaderText = "รหัสการชำระเงิน";
            grvPayment.Columns[1].DataPropertyName = "PAYMENTS_ID";
            grvPayment.Columns[1].Width = 110;

            grvPayment.Columns[2].Name = "PAYMENTS_DUE_DATE";
            grvPayment.Columns[2].HeaderText = "กำหนดชำระเงิน";
            grvPayment.Columns[2].DataPropertyName = "PAYMENTS_DUE_DATE";
            grvPayment.Columns[2].Width = 110;

            grvPayment.Columns[3].Name = "CUSTOMER_NAME";
            grvPayment.Columns[3].HeaderText = "ชื่อ - นามสกุล";
            grvPayment.Columns[3].DataPropertyName = "CUSTOMER_NAME";
            grvPayment.Columns[3].Width = 130;

            grvPayment.Columns[4].Name = "LOAN_ID";
            grvPayment.Columns[4].HeaderText = "รหัสการขอกู้";
            grvPayment.Columns[4].DataPropertyName = "LOAN_ID";
            grvPayment.Columns[4].Width = 90;

            grvPayment.Columns[5].Name = "PAY_MONTH";
            grvPayment.Columns[5].HeaderText = "ยอดที่ชำระ";
            grvPayment.Columns[5].DataPropertyName = "PAY_MONTH";
            grvPayment.Columns[5].Width = 85;

            grvPayment.Columns[6].Name = "PAYMENTS_STATUS";
            grvPayment.Columns[6].HeaderText = "สถานะ";
            grvPayment.Columns[6].DataPropertyName = "PAYMENTS_STATUS";
            grvPayment.Columns[6].Width = 50;
            // end gen gridview
        }

        private DataTable LoadPayment()
        {
            DataTable dt = DAL.PaymentInfoDAL.LoadAllPayment(this.txtPerId.Text, this.dtpDate.Value.Year + "-" + this.dtpDate.Value.Month + "-" + this.dtpDate.Value.Day);
            if (dt != null && dt.Rows[0]["PAYMENTS_STATUS"].ToString().Equals("I"))
            {
                MessageBox.Show("รายการชำระเงินนี้ชำระเงินเรียบร้อยแล้ว", "ข้อมูลการชำระเงิน");
                return null;
            }
            return dt;
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxColumn))
            {
                //frmPayments frmPayments = new frmPayments(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), empItem.EMPLOYEE_ID);
                //frmPayments.ShowDialog();
                //grvPayment.DataSource = LoadPayment();
                //((DataGridView)sender).Rows[e.RowIndex].Cells[0]. = true;
                DataGridViewCheckBoxCell cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                if (cell.Value != null)
                {
                    if ((bool)cell.Value == false)
                        cell.Value = true;
                    else
                        cell.Value = false;
                }
                else 
                {
                    cell.Value = true;
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtPerId.Text != "")
            {
                grvPayment.DataSource = LoadPayment();
            }
            else
            {
                MessageBox.Show("กรุณากรอกรหัสประจำตัวประชาชน", "กรอกข้อมูล");
            }
        }

        private void btnPayment_Click(object sender, EventArgs e)
        {
            bool[] myArray = new bool[grvPayment.Rows.Count];
            int i = 0;
            foreach (DataGridViewRow row in grvPayment.Rows)
            {
                DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;
                if (cell.Value != null)
                {
                    myArray[i] = (bool)cell.Value;
                }
                else 
                {
                    myArray[i] = false;
                }
                i++;
            }

            bool checkAll = true;
            for (int j = 0; j < grvPayment.Rows.Count; j++)
            {
                if (!myArray[j])
                {
                    checkAll = false;
                }
            }

            if (checkAll)
            {
                int[] arrayId = new int[grvPayment.Rows.Count];
                int j = 0;
                foreach (DataGridViewRow row in grvPayment.Rows)
                {
                    arrayId[j] = Convert.ToInt32((grvPayment.Rows[j].Cells[1].Value.ToString()));
                    j++;
                }
                frmPayments frmPayments = new frmPayments(arrayId, empItem.EMPLOYEE_ID);
                frmPayments.ShowDialog();
            }
            else 
            {
                if (myArray[0])
                {
                    foreach (DataGridViewRow row in grvPayment.Rows)
                    {
                        DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;
                        if (cell.RowIndex != 0)
                        {
                            cell.Value = false;
                        }
                    }
                    frmPayments frmPayments = new frmPayments(Convert.ToInt32((grvPayment.Rows[0].Cells[1].Value.ToString())), empItem.EMPLOYEE_ID);
                    frmPayments.ShowDialog();
                }
                else 
                {
                    MessageBox.Show("หากไม่ชำระทั้งหมด กรุณาชำระงวดเดือนแรก", "เลือกข้อมูลชำระเงิน");
                    return;
                }
            }
        }
    }
}
